export default {
	data() {
		return {
			nickname: "",
			avatarUrl: "",
			genderArr: ['男', '女'],
			birthday: null,
			gender: null,
			region: null, // 积分数组 
		}
	},
	async onLoad() {
		await this.getUserInfo();
	},
	methods: {
		async getUserInfo() {
			const getRes = await this.$uniCloudCallFunction({
				name: "xxf-user",
				action: "getUser",
				data: {
					uid: this.$store.state.login.uid,
				}
			});
			console.log("getRes: ", getRes);
			if (getRes) {
				this.nickname = getRes.nickname;
				this.avatarUrl = getRes.avatar_url;
				this.birthday = getRes.birthday;
				this.gender = getRes.gender;
				this.region = [getRes.province, getRes.city, getRes.district];
				this.region = this.region.filter(item => item !== '' && item !== null && item !== undefined && item === item);
				this.$store.commit("login/setLoginInfo", {
					avatarUrl: getRes.avatar_url,
					nickname: getRes.nickname,
				});
			}
		},
		async onChooseAvatar(e) {
			console.log("onChooseAvatar:", e.detail);
			this.$store.commit('showLoading');
			const tArr = e.detail.avatarUrl.split('/');
			const result = await uniCloud.uploadFile({
				filePath: e.detail.avatarUrl,
				cloudPath: `user_avatar/${tArr[tArr.length - 1]}`,
				onUploadProgress: function(progressEvent) {
					console.log(progressEvent);
					let percentCompleted = Math.round(
						(progressEvent.loaded * 100) / progressEvent.total
					);
				}
			});
			console.log("uploadFile result: ", result);
			const urlRes = await uniCloud.getTempFileURL({
				fileList: [result.fileID],
			});
			console.log("getTempFileURL avatar: ", urlRes.fileList[0].tempFileURL);
			const updateRes = await this.$uniCloudCallFunction({
				name: "xxf-user",
				action: "updateUser",
				data: {
					uid: this.$store.state.login.uid,
					avatar_url: urlRes.fileList[0].tempFileURL,
				}
			});
			console.log("updateRes: ", updateRes);
			if (updateRes) {
				this.avatarUrl = urlRes.fileList[0].tempFileURL;
				this.$store.commit("login/setLoginInfo", {
					...this.$store.state.login,
					avatarUrl: updateRes.avatar_url,
				});
				await this.getUserInfo();
			}
			setTimeout(() => this.$store.commit('hideLoading'), 500);
		},
		getNickname(e) {
			console.log("getNickname: ", e);
			this.nickname = e.detail.value;
		},
		bindDateChange: function(e) {
			this.birthday = e.detail.value;
		},
		bindGenderChange(e) {
			console.log("bindGenderChange: ", e);
			this.gender = e.detail.value;
		},
		bindRegionChange(e) {
			console.log("bindRegionChange: ", e);
			this.region = e.detail.value;
			// country: '', // 国家
			// province: '', // 省份
			// city: '', // 城市
			// district: '', // 区（县）
		},
		async saveUserInfo() {
			console.log("头像: ", this.avatarUrl);
			console.log("昵称: ", this.nickname);
			console.log("生日: ", this.birthday);
			console.log("性别: ", this.gender);
			console.log("地区: ", this.region);
			if (await this.$utils.showModal(this, {
					title: '温馨提示',
					content: '确定修改提交？',
				})) {
				const updateRes = await this.$uniCloudCallFunction({
					name: "xxf-user",
					action: "updateUser",
					data: {
						uid: this.$store.state.login.uid,
						nickname: this.nickname,
						birthday: this.birthday,
						gender: this.gender,
						country: '中国',
						province: this.region[0],
						city: this.region[1],
						district: this.region[2],
					}
				});
				if (updateRes) {
					this.$store.commit("login/setLoginInfo", {
						...this.$store.state.login,
						avatarUrl: this.avatarUrl,
						nickname: this.nickname,
						birthday: this.birthday,
						gender: this.gender,
						country: '中国',
						province: this.region[0],
						city: this.region[1],
						district: this.region[2],
					});
					await this.$utils.showToast(this, {
						message: '更新成功',
						mode: 1,
					})
					uni.navigateBack();
				}
			}
		},
	}
}